import 'package:exsun_plate_app/common/constant.dart';
import 'package:flutter/material.dart';
import 'package:common_utils/common_utils.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SplashAppPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _SplashPageState();
  }
}

class _SplashPageState extends State<SplashAppPage> {
  TimerUtil _timerUtil;
  int _status = 0;
  int _count = 3;

  Future _goMain() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var isLogin = prefs.getBool(Constants.IS_LOGIN);
    if (isLogin == null||isLogin==false) {
      Navigator.of(context).pushReplacementNamed('/LoginPage');
    } else {
      Navigator.of(context).pushReplacementNamed('/HomePage');
    }
  }

  void _doCountDown() {
    setState(() {
      _status = 1;
    });

    _timerUtil = new TimerUtil(mTotalTime: 2 * 1000);
    _timerUtil.setOnTimerTickCallback((int tick) {
      double _tick = tick / 1000;
      setState(() {
        _count = _tick.toInt();
      });
      if (_tick == 0) {
        _goMain();
      }
    });
    _timerUtil.startCountDown();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _doCountDown();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    if (_timerUtil != null) {
      _timerUtil.cancel();
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
        decoration: BoxDecoration(
            image: DecorationImage(
          image: AssetImage('images/app_splash_bg.png'),
          fit: BoxFit.cover,
        )),
        child: Scaffold(
            backgroundColor: Colors.transparent, //把scaffold的背景色改成透明
            appBar: AppBar(
              backgroundColor: Colors.transparent, //把appbar的背景色改成透明
              elevation: 0, //appbar的阴影
            ),
            body: new Stack(
              children: <Widget>[
                Center(),
                new Positioned(
                    bottom: 60,
                    left: 0,
                    right: 0,
                    child: new Image.asset('images/app_splash_logo.png'))
              ],
            )));
  }
}

class SplashPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      body: new SplashAppPage(),
    );
  }
}
